'use strict';

/*
*				Main Controller
*/
shopApp.controller('MainCtrl', function($scope,mainProductsList, mainPromoList,mainCategory, $http, $templateCache,$location, $timeout) {


	//.. Products
	// get products
	$scope.list = mainProductsList.query();
	// get promos
	$scope.promos = mainPromoList.query();
	// get categories
	$scope.categories = mainCategory.query();
	// Product Detail
	$scope.Message = "Product Detail";
	$scope.Product = {
		Name:"", Description:"", Price: 0, Image:"", Item: null
	};
	$scope.setProduct = function(item){
		$scope.Product.name = item.name;
		$scope.Product.description =  item.description;
		$scope.Product.price = item.price;
		$scope.Product.image = item.imagePath;
		$scope.Product.item = item;
	};


	//.. User Validation
	$scope.showLoginMessage =  false;
	$scope.errorLoginDetail = "verify user and password and try again";
	$scope.boxUser = {
		user:"",pass:""
	};	
	// clean error message
	$scope.cleanErrorMessage  = function(){
		$scope.showLoginMessage =  false;
	}
//**end
});


/*
*				Product Controller
*/
shopApp.controller('ProductsCtrl', function($scope,ProductsRest, $routeParams){
	//..
	$scope.productName = "";
	$scope.currentPage = 0;
	$scope.bound = 12;
	$scope.totalPages = [{pageidx:1}];
	$scope.initRecord = 0;
	var current = $scope.currentPage;

	//..
	$scope.getPage =  function(idx){
		var init = $scope.bound * idx ;
		//if (idx>0){init+=1;}
		$scope.initRecord =  init;
		$scope.list =  ProductsRest.query({"init-rec":init, "end-rec":$scope.bound },function(){
			$scope.getTotalPages($scope.list[0].records);	
			$scope.productName = "";
		});
		
	};
	$scope.getTotalPages = function(rows){
		$scope.totalPages = [];
		for (var i = 1; i <= parseInt(rows/12) + ((rows%12>0)?1:0); i++) {
			$scope.totalPages.push({pageidx:i});
		};
	};
	$scope.changePage= function(idx){
		$scope.getPage(idx);
		$scope.currentPage = idx;
	};
	$scope.nextPage =  function(){
		if ($scope.currentPage == ($scope.totalPages.length -1)){return;}
		$scope.currentPage +=1;
		$scope.getPage($scope.currentPage);

	};
	$scope.prevPage = function(){
		if ($scope.currentPage == 0){return;}
		$scope.currentPage -=1;
		$scope.getPage($scope.currentPage);
	};

	//..
	$scope.getPage(0);


	//..
	$scope.Product = {
		Name:"", Description:"", Price: 0, Image:"", Item: null
	};
	$scope.setProduct = function(item){
		$scope.Product.Name = item.Name;
		$scope.Product.Description =  item.Description;
		$scope.Product.Price = item.Price;
		$scope.Product.Image = item.ImagePath;
		$scope.Product.Item = item;
	};

});


/*
*				Cart Controller
*/
shopApp.controller('CartCtrl',function($scope){

	// set filter
	$scope.cartFilter = {completed : true};

});
